package com.homwee.factory;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FactoryToast extends Toast {
	/**
	 * 显示的文本框
	 */
	public TextView tv;
	private Toast mToast=this;
	/**
	 * 显示窗口的持续时
	 */
	private int mDuration;
	private Context mContext;
	/**
	 * 用户的提示信
	 */
	private String mMessageText="";
	/**
	 * 显示图标
	 */
	private ImageView FactoryToastIcon;
	public FactoryToast(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext=context;
		initView();
	}
	private void initView() {
		LayoutInflater layout = (LayoutInflater) mContext
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = layout.inflate(R.layout.factory_toast,null);
		FactoryToastIcon=(ImageView)(view.findViewById(R.id.factory_toast_icon));
		tv = (TextView) (view.findViewById(R.id.factory_toast_message));
		tv.setText(getMessage());
		this.setView(view);
		this.setGravity(Gravity.CENTER, 0, 0);
	}
	public void setDuration(int duration) {
		
		this.mDuration=duration;
	}
	
	public void setMessage(String message) {
		this.mMessageText = message;
		tv.setText(message);
	}
	/**
	 * 获取用户的提示信
	 * @return the mHintMessage
	 */
	public String getMessage() {
		return mMessageText;
	}
	public void show() {
		
		super.show();
		startTimeTask();
		
	}
	private void startTimeTask() {

		   final Timer timer = new Timer();
		   TimerTask task = new TimerTask() {
			public void run() {
				// what to do()
				if(mToast!=null&&mDuration>0)
				{ 
				   
					show();
					mDuration--;
//					Log.v("chFactory", "mDuration:"+mDuration);
				}
				else
				{
					if(mToast != null){
					   mToast.cancel();
//					   Log.v("chFactory111", "mToast.cancel();");
					}	
					timer.cancel();
//					Log.v("chFactory", "mToast.cancel();");
				}
			}
		};
		timer.schedule(task, 1 * 1000, 1 * 1000);
    
		
	}
	public TextView getTextView(){
		return tv;
	}
	public void setToastIcon(int resID){
		if(resID > 0){
			FactoryToastIcon.setImageResource(resID);
		}
	}
	public void setToastIcon(Drawable drawable){
		FactoryToastIcon.setImageDrawable(drawable);
	}
}